package com.example.appointment.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.appointment.R;
import com.example.appointment.meta.DistrictsBean;
import com.example.appointment.util.Constant;

import java.util.ArrayList;

/**
 * 选择地区的适配器
 * Created by hehe on 2016/7/5.
 */
public class ChooseDistriAdapter extends RecyclerView.Adapter<ChooseDistriAdapter.MyViewHolder> {
    private Context context;
    private ArrayList<DistrictsBean> arrayList;
    private Handler mHander;

    public void removeitem(DistrictsBean item) {
        int index = arrayList.indexOf(item);
        arrayList.remove(item);
        notifyItemRemoved(index);
    }

    public void clearall() {
        arrayList.clear();
        notifyDataSetChanged();
    }

    public void additem(DistrictsBean item) {

        this.arrayList.add(item);
        int index = arrayList.indexOf(item);
        notifyItemInserted(index);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                context).inflate(R.layout.item_choosearea, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final DistrictsBean itemvo = arrayList.get(position);
        holder.areatext.setText(itemvo.getDisName());
        holder.choosearea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg = new Message();
                msg.what = Constant.ONITEM_CLICK;
                msg.obj = itemvo;
                mHander.sendMessage(msg);
            }
        });
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public ArrayList<DistrictsBean> getArrayList() {
        return arrayList;
    }

    public void setArrayList(ArrayList<DistrictsBean> arrayList) {
        this.arrayList = arrayList;
    }

    public ChooseDistriAdapter(Context context, Handler mHander) {
        this.context = context;
        this.mHander = mHander;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView areatext;//选择地区的文本
        ImageView enter;//进入下一个界面的图标
        View downdivider;//最下面的下划线
        RelativeLayout choosearea;//选择地区的选项

        public MyViewHolder(View itemView) {
            super(itemView);
            areatext = (TextView) itemView.findViewById(R.id.item_province_name);
            enter = (ImageView) itemView.findViewById(R.id.item_province_enter);
            downdivider = itemView.findViewById(R.id.item_province_divier);
            choosearea = (RelativeLayout) itemView.findViewById(R.id.item_choosearea_layout);
        }
    }

}
